package com.aier.common.common;

import java.util.List;
import java.util.TimerTask;

import javax.annotation.Resource;

import cn.emay.sdk.client.api.MO;


import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.InviteParent;
import com.aier.classes.entity.InviteTeacher;
import com.aier.classes.service.IInviteRequestService;
import com.aier.common.service.ISysPhoneMessageService;



public class SysTimeGetPhoneMessage extends TimerTask {
//    private Integer error_max_size;
    
    @Resource(name = "inviteRequestService")
    private IInviteRequestService inviteRequestService;
    
    @Resource(name = "sysPhoneMessageService")
    private ISysPhoneMessageService sysPhoneMessageService;    

    /**
     * 定时查看接收到的短信
     */

    @Override
    public void run() {
        try {
            List<MO> list = MessageSingletonClient.getClient().getMO();
            if (list != null) {
//                System.out.println("testGetMO1size:" + list.size());
                for(MO mo : list) {
                    String phone = mo.getMobileNumber();
                    String message = mo.getSmsContent();
                    sysPhoneMessageService.insertGetPhoneMessage(phone,message);
                    System.out.println("getmessage,phone:" + phone + ",message:" + message);
//                    System.out.println("短信内容:" + mo.getSmsContent());
//                    System.out.println("通道号:" + mo.getChannelnumber());
//                    System.out.println("手机号:" + mo.getMobileNumber());
//                    System.out.println("附加码:" + mo.getAddSerialRev());
//                    System.out.println("附加码:" + mo.getAddSerial());
                    // 上行短信务必要保存,加入业务逻辑代码,如：保存数据库，写文件等等
                    if("PY".equals(message.trim().toUpperCase())){//家长同意邀请
                        InviteParent ip = inviteRequestService.searchInviteParentByPhone(phone,InviteRequestUtil.INVITE_TYPE_WAIT);
                        if(ip!=null){
                            inviteRequestService.updateInviteParent(ip.getIpid(), InviteRequestUtil.INVITE_TYPE_AGREE);
                        }
                    }else if("TY".equals(message.trim().toUpperCase())){//教师同意邀请
                        InviteTeacher it = inviteRequestService.searchInviteTeacherByPhone(phone,InviteRequestUtil.INVITE_TYPE_WAIT);
                        if(it!=null){
                            inviteRequestService.updateInviteTeacher(it.getItid(), InviteRequestUtil.INVITE_TYPE_AGREE);
                        }
                    }else{//拒绝
                        inviteRequestService.updateInviteRefuse(phone);
                    }
                    
                }
            } else {
                System.out.println("do SysTimeGetPhoneMessage ：NO HAVE MO");
            }
            //默认都同意
//            List<InviteParent> ipList = inviteRequestService.searchInviteParentListByState(InviteRequestUtil.INVITE_TYPE_WAIT);
//            if(ipList!=null && ipList.size()>0){
//            	for(InviteParent ip:ipList){
//            		inviteRequestService.updateInviteParent(ip.getIpid(), InviteRequestUtil.INVITE_TYPE_AGREE);
//            	}
//            }
//            List<InviteTeacher> itList = inviteRequestService.searchInviteTeacherListByState(InviteRequestUtil.INVITE_TYPE_WAIT);
//            if(itList!=null && itList.size()>0){
//            	for(InviteTeacher it:itList){
//            		inviteRequestService.updateInviteTeacher(it.getItid(), InviteRequestUtil.INVITE_TYPE_AGREE);
//            	}
//            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setInviteRequestService(IInviteRequestService inviteRequestService) {
        this.inviteRequestService = inviteRequestService;
    }


    public void setSysPhoneMessageService(
            ISysPhoneMessageService sysPhoneMessageService) {
        this.sysPhoneMessageService = sysPhoneMessageService;
    }

    





    
}
